package hackathon.light.up;

import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.Log;

public class Mirror extends Element {

	private boolean visited;

	public Mirror(int row, int col, LightDirection dir) {
		super(row, col, dir);
		
	}

	@Override
	public void reset() {
		allowedDirections.clear();
		super.allowedDirections
		.add(LightDirection.values()[(orientation.ordinal() + 3) % 8]);
super.allowedDirections
		.add(LightDirection.values()[(orientation.ordinal() + 5) % 8]);
lightSources.clear();
super.lightSources.add(new LightSource(row, col, LightDirection.SE, 2));
super.lightSources.get(0).flagOn = false;
super.flagOnScene = true;
visited = false;
	}

	@Override
	public void update(LightSource lightSource) {
		// TODO Auto-generated method stub
		if (flagOnScene) {
			if (!visited) {

				for (int i = 0; i < allowedDirections.size(); i++) {

					Log.d("MIRROR", allowedDirections.get(i) + " "
							+ lightSource.direction);

					if (allowedDirections.get(i) == lightSource.direction) {

						visited = true;
						for (int j = 0; j < lightSources.size(); j++) {
							if (!lightSources.get(j).flagOn) {
								lightSources.get(j).flagOn = true;
								lightSources.get(j).color_id = lightSource.color_id;
								lightSources.get(j).direction = LightDirection
										.values()[(super.orientation.ordinal() + 1) % 8];
								if (i == 1) {
									lightSources.get(j).direction = LightDirection
											.values()[(super.orientation
											.ordinal() + 7) % 8];
								}

								int next_col = col
										+ MapDirection.getNextCol(lightSources
												.get(j).direction);
								int next_row = row
										+ MapDirection.getNextRow(lightSources
												.get(j).direction);
								while (Grid.elementMatrix[next_row + 1][next_col + 1] == null) {
									next_col += MapDirection
											.getNextCol(lightSources.get(j).direction);
									next_row += MapDirection
											.getNextRow(lightSources.get(j).direction);
								}
								lightSources.get(j).drawLight(next_row,
										next_col);
								Grid.elementMatrix[next_row + 1][next_col + 1]
										.update(lightSources.get(j));
							}
						}
					}
				}
			}
		}
	}

}
